import {nameComponentObj} from "@/assets/menu/index.js"

const state = {
  iframeList: [], //缓存的iframelist 存储的是 meta对象
  visitedViews: [], //当前选中的标签  存储的是route对象
  cachedViews: [] //设置缓存的菜单页签  存储的是 route对象
}

const mutations = {
  // 添加访问过的标签
  ADD_VISITED_VIEW: (state, view) => {
    if (state.visitedViews.some(v => v.fullPath === view.fullPath)) { return }
    if (view.meta.affix) { //如果是固定标签 添加在前面
      state.visitedViews.unshift(
        // 加个 {} 目的： 不影响view数据
        Object.assign({
        }, view, {
          title: view.meta.title || "no-name"
        })
      )
    } else {
      state.visitedViews.push(
        // 加个 {} 目的： 不影响view数据
        Object.assign({
        }, view, {
          title: view.meta.title || "no-name"
        })
      )
    }

    // 首页放到第一个展示
    const hasDashboardIndex = state.visitedViews.findIndex(item => item.name === "dashboard")
    if (hasDashboardIndex !== -1) {
      if (hasDashboardIndex !== 0) { // 将首页放在第一位
        const dashboardViewArr = state.visitedViews.splice(hasDashboardIndex, 1)
        const dashboardView = dashboardViewArr[0]
        state.visitedViews.unshift(dashboardView)
      }
    } else {
      state.visitedViews.unshift(Object.assign({
      }, nameComponentObj["dashboard"], {
        title: nameComponentObj["dashboard"].meta.title || "no-name"
      }))
    }

  },
  // 添加缓存页面
  ADD_CACHED_VIEW: (state, view) => {
    const cachedViewsList = state.cachedViews.map(item => { return item.name })
    if (cachedViewsList.includes(view.name)) { return }
    if (view.meta.cache === "1") {
      state.cachedViews.push(view)
    }
  },

  //添加iframe缓存
  ADD_IFRAME_LIST: (state, view) => {
    const iframeList = state.iframeList.map(item => { return item.meta.iframeSrc })
    if (iframeList.includes(view.meta.iframeSrc)) { return }
    if (view.meta.cache === "1") {
      state.iframeList.push(view)
    }
  },
  // 删除访问过的标签页
  DEL_VISITED_VIEW: (state, view) => {
    for (const [i, v] of state.visitedViews.entries()) {
      if (v.fullPath === view.fullPath) {
        state.visitedViews.splice(i, 1)
        break
      }
    }
  },
  // 删除iframe
  DEL_IFRAME_LIST: (state, view) => {
    if (view.meta?.iframeSrc) {
      for (const [i, v] of state.iframeList.entries()) {
        if (v.meta.iframeSrc === view.meta.iframeSrc) {
          state.iframeList.splice(i, 1)
          break
        }
      }
    }
  },
  // 删除缓存页面
  DEL_CACHED_VIEW: (state, view) => {
    const cachedViewsList = state.cachedViews.map(item => { return item.name })
    const index = cachedViewsList.indexOf(view.name)
    index > -1 && state.cachedViews.splice(index, 1)
  },
  // 删除其他标签
  DEL_OTHERS_VISITED_VIEWS: (state, view) => {
    state.visitedViews = state.visitedViews.filter(v => {
      return v.meta.affix || v.fullPath === view.fullPath
    })
  },
  // 删除其他缓存的
  DEL_OTHERS_CACHED_VIEWS: (state, view) => {
    state.cachedViews = state.cachedViews.filter(v => {
      return (v.meta.affix && v.meta.cache === "1") || v.fullPath === view.fullPath
    })
  },
  // 删除其他缓存的iframe列表
  DEL_OTHERS_IFRAME_LISTS: (state, view) => {
    state.iframeList = state.iframeList.filter(v => {
      return (v.meta.affix && v.meta.cache === "1" && v.meta?.iframeSrc) || v.fullPath === view.fullPath
    })
  },
  //删除所有 访问过的页面 affix为true的除外
  DEL_ALL_VISITED_VIEWS: state => {
    // keep affix tags
    const affixTags = state.visitedViews.filter(tag => tag.meta.affix)
    state.visitedViews = affixTags
  },
  //删除所有 访问过的页面 包括 affix为true
  DEL_ALL_VISITED_VIEWS_INCLUDE_AFFIX: state => {
    state.visitedViews = []
  },
  // 删除所有缓存路由 不包含 affix
  DEL_ALL_CACHED_VIEWS: state => {
    // keep affix tags
    const affixTags = state.cachedViews.filter(tag => tag.meta.affix && tag.meta.cache === "1")
    state.cachedViews = affixTags
  },
  // 删除所有缓存路由 包含 affix
  DEL_ALL_CACHED_VIEWS_INCLUDE_AFFIX: state => {
    state.cachedViews = []
  },
  // 删除所有缓存IFRAME 不包含 affix
  DEL_ALL_IFRAME_LISTS: state => {
    // keep affix tags
    const affixTags = state.iframeList.filter(tag => tag.meta.affix && tag.meta.cache === "1" && tag.meta.iframeSrc)
    state.iframeList = affixTags
  },
  // 删除所有缓存IFRAME 包含 affix
  DEL_ALL_IFRAME_LISTS_INCLUDE_AFFIX: state => {
    state.iframeList = []
  },
  //根据传入的 view对象 更新
  UPDATE_VISITED_VIEW: (state, view) => {
    for (let v of state.visitedViews) {
      if (v.fullPath === view.fullPath) {
        v = Object.assign(v, view)
        break
      }
    }
  },
  // 通过name删除缓存页面
  DEL_CACHED_VIEW_BY_NAME: (state, name) => {
    state.cachedViews = state.cachedViews.filter(item => {
      return item.name !== name
    })
  },

  // 通过fullPath和新标签名 设置 tag标签名
  SET_TAG_TITLE_BY_FULL_PATH: (state, obj) => {
    for (const v of state.visitedViews) {
      if (v.fullPath === obj.fullPath) {
        v.title = obj.title
        break
      }
    }
  }
}

const actions = {
  addIframeList({commit}, src) {
    commit("ADD_IFRAME_LIST", src)
  },
  addView({ dispatch }, view) {
    dispatch("addVisitedView", view)
    dispatch("addCachedView", view)
  },
  addVisitedView({ commit }, view) {
    commit("ADD_VISITED_VIEW", view)
  },
  addCachedView({ commit }, view) {
    commit("ADD_CACHED_VIEW", view)
  },

  delView({ dispatch, state }, view) {
    return new Promise(resolve => {
      dispatch("delVisitedView", view)
      dispatch("delCachedView", view)
      dispatch("delIframeList", view)
      resolve({
        visitedViews: [...state.visitedViews],
        cachedViews: [...state.cachedViews]
      })
    })
  },
  delVisitedView({ commit, state }, view) {
    return new Promise(resolve => {
      commit("DEL_VISITED_VIEW", view)
      resolve([...state.visitedViews])
    })
  },
  // 删除缓存的路由 及 缓存的  iframe
  delCachedView({ commit, state }, view) {
    return new Promise(resolve => {
      commit("DEL_CACHED_VIEW", view)
      commit("DEL_IFRAME_LIST", view)
      resolve([...state.cachedViews])
    })
  },
  delIframeList({ commit }, view) {
    commit("DEL_IFRAME_LIST", view)
  },

  delOthersViews({ dispatch, state }, view) {
    return new Promise(resolve => {
      dispatch("delOthersVisitedViews", view)
      dispatch("delOthersCachedViews", view)
      dispatch("delOthersIframeLists", view)
      resolve({
        visitedViews: [...state.visitedViews],
        cachedViews: [...state.cachedViews]
      })
    })
  },
  delOthersVisitedViews({ commit, state }, view) {
    return new Promise(resolve => {
      commit("DEL_OTHERS_VISITED_VIEWS", view)
      resolve([...state.visitedViews])
    })
  },
  delOthersCachedViews({ commit, state }, view) {
    return new Promise(resolve => {
      commit("DEL_OTHERS_CACHED_VIEWS", view)
      resolve([...state.cachedViews])
    })
  },
  delOthersIframeLists({ commit }, view) {
    commit("DEL_OTHERS_IFRAME_LISTS", view)
  },
  //删除 访问过的页面(affix为true的除外)及缓存页面
  delAllViews({ dispatch, state }, view) {
    return new Promise(resolve => {
      dispatch("delAllVisitedViews", view)
      dispatch("delAllCachedViews", view)
      dispatch("delAllIframeLists", view)
      resolve({
        visitedViews: [...state.visitedViews],
        cachedViews: [...state.cachedViews]
      })
    })
  },
  //删除 访问过的页面(包括affix为true)及缓存页面
  delAllViewsIncludeAffix({ dispatch, state }, view) {
    console.log("删除 访问过的页面(包括affix为true)及缓存页面")
    return new Promise(resolve => {
      dispatch("delAllVisitedViewsIncludeAffix", view)
      dispatch("delAllCachedViewsIncludeAffix", view)
      dispatch("delAllIframeListsIncludeAffix", view)
      resolve({
        visitedViews: [...state.visitedViews],
        cachedViews: [...state.cachedViews]
      })
    })
  },
  //删除所有访问过的 页面 affix为true的除外
  delAllVisitedViews({ commit, state }) {
    return new Promise(resolve => {
      commit("DEL_ALL_VISITED_VIEWS")
      resolve([...state.visitedViews])
    })
  },
  delAllCachedViews({ commit, state }) {
    return new Promise(resolve => {
      commit("DEL_ALL_CACHED_VIEWS")
      resolve([...state.visitedViews])
    })
  },
  delAllIframeLists({ commit, state }) {
    return new Promise(resolve => {
      commit("DEL_ALL_IFRAME_LISTS")
      resolve([...state.visitedViews])
    })
  },
  //删除所有访问过的 页面 affix为true的除外
  delAllVisitedViewsIncludeAffix({ commit, state }) {
    return new Promise(resolve => {
      commit("DEL_ALL_VISITED_VIEWS_INCLUDE_AFFIX")
      resolve([...state.visitedViews])
    })
  },
  // 删除所有缓存的 页面
  delAllCachedViewsIncludeAffix({ commit, state }) {
    return new Promise(resolve => {
      commit("DEL_ALL_CACHED_VIEWS_INCLUDE_AFFIX")
      resolve([...state.cachedViews])
    })
  },
  delAllIframeListsIncludeAffix({ commit, state }) {
    return new Promise(resolve => {
      commit("DEL_ALL_IFRAME_LISTS_INCLUDE_AFFIX")
      resolve([...state.cachedViews])
    })
  },
  //根据传入的view对象 更新视图
  updateVisitedView({ commit }, view) {
    commit("UPDATE_VISITED_VIEW", view)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
